package com.amazon.ags.client.whispersync.savedgame;

import android.content.Context;
import android.util.Log;
import com.amazon.ags.client.whispersync.GameSummary;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;

public class FileBackedSummaryRepository
  implements SummaryRepository
{
  public static final String AMAZON_GAMES_DIR_NAME = ".amazonGamesService";
  private static final String PENDING_DOWNLOAD_DATA_FILE_NAME = "pendingDownloadData.dat";
  private static final String PENDING_DOWNLOAD_SUMMARY_FILE_NAME = "pendingDownloadSummary.dat";
  private static final String PENDING_UPLOAD_DATA_FILE_NAME = "pendingUploadData.dat";
  private static final String PENDING_UPLOAD_DESCRIPTION_FILE_NAME = "pendingUploadDescription.txt";
  private static final String SUMMARY_FILE_NAME = "latestSummary.txt";
  private static final String TAG = "AG_WS_" + FileBackedSummaryRepository.class.getSimpleName();
  private final Context context;
  private final SummaryMarshaller marshaller;

  public FileBackedSummaryRepository(Context paramContext, SummaryMarshaller paramSummaryMarshaller)
  {
    this.context = paramContext;
    this.marshaller = paramSummaryMarshaller;
  }

  private File getAmazonGamesDir()
  {
    File localFile = new File(this.context.getFilesDir().getParentFile(), ".amazonGamesService");
    localFile.mkdirs();
    return localFile;
  }

  private Date lastModified(String paramString)
  {
    File localFile = new File(getAmazonGamesDir(), paramString);
    if (localFile.exists())
      return new Date(localFile.lastModified());
    return null;
  }

  private byte[] readFromAmazonGamesDir(String paramString)
    throws IOException
  {
    File localFile = new File(getAmazonGamesDir(), paramString);
    if (!localFile.exists())
      return null;
    RandomAccessFile localRandomAccessFile = new RandomAccessFile(localFile, "r");
    try
    {
      byte[] arrayOfByte = new byte[(int)localRandomAccessFile.length()];
      localRandomAccessFile.readFully(arrayOfByte);
      return arrayOfByte;
    }
    finally
    {
      localRandomAccessFile.close();
    }
  }

  private GameSummary readGameSummary(String paramString)
  {
    File localFile = new File(getAmazonGamesDir(), paramString);
    try
    {
      FileReader localFileReader = new FileReader(localFile);
      try
      {
        String str = new BufferedReader(localFileReader).readLine();
        GameSummary localGameSummary = this.marshaller.unmarshal(str);
        return localGameSummary;
      }
      finally
      {
        localFileReader.close();
      }
    }
    catch (IOException localIOException)
    {
    }
    return null;
  }

  private void writeGameSummary(GameSummary paramGameSummary, String paramString)
  {
    String str = this.marshaller.marshal(paramGameSummary);
    if (str != null);
    try
    {
      writeToAmazonGamesDir(paramString, str.getBytes());
      return;
    }
    catch (IOException localIOException)
    {
      Log.e(TAG, "Could not write game summary", localIOException);
    }
  }

  private void writeToAmazonGamesDir(String paramString, byte[] paramArrayOfByte)
    throws IOException
  {
    FileOutputStream localFileOutputStream = new FileOutputStream(new File(getAmazonGamesDir(), paramString));
    try
    {
      localFileOutputStream.write(paramArrayOfByte);
      return;
    }
    finally
    {
      localFileOutputStream.close();
    }
  }

  public final boolean hasPendingDownload()
  {
    return new File(getAmazonGamesDir(), "pendingDownloadData.dat").exists();
  }

  public final void removePendingDownload()
  {
    new File(getAmazonGamesDir(), "pendingDownloadData.dat").delete();
    new File(getAmazonGamesDir(), "pendingDownloadSummary.dat").delete();
  }

  public final void removePendingUpload()
  {
    File localFile1 = new File(getAmazonGamesDir(), "pendingUploadData.dat");
    File localFile2 = new File(getAmazonGamesDir(), "pendingUploadDescription.txt");
    localFile1.delete();
    localFile2.delete();
  }

  public final void removeSummary()
  {
    new File(getAmazonGamesDir(), "latestSummary.txt").delete();
  }

  public final PendingDownload retrievePendingDownload()
    throws IOException
  {
    byte[] arrayOfByte = readFromAmazonGamesDir("pendingDownloadData.dat");
    GameSummary localGameSummary = readGameSummary("pendingDownloadSummary.dat");
    if (arrayOfByte == null)
      return null;
    return new PendingDownload(arrayOfByte, localGameSummary);
  }

  public final PendingUpload retrievePendingUpload()
    throws IOException
  {
    byte[] arrayOfByte1 = readFromAmazonGamesDir("pendingUploadData.dat");
    byte[] arrayOfByte2 = readFromAmazonGamesDir("pendingUploadDescription.txt");
    if (arrayOfByte2 == null);
    for (String str = null; ; str = new String(arrayOfByte2))
      return new PendingUpload(arrayOfByte1, str, lastModified("pendingUploadData.dat"));
  }

  public final GameSummary retrieveSummary()
  {
    return readGameSummary("latestSummary.txt");
  }

  public final void storePendingDownload(PendingDownload paramPendingDownload)
    throws IOException
  {
    writeToAmazonGamesDir("pendingDownloadData.dat", paramPendingDownload.getData());
    writeGameSummary(paramPendingDownload.getSummary(), "pendingDownloadSummary.dat");
  }

  public final void storePendingUpload(PendingUpload paramPendingUpload)
    throws IOException
  {
    writeToAmazonGamesDir("pendingUploadData.dat", paramPendingUpload.getData());
    if (paramPendingUpload.getDescription() == null)
    {
      new File(getAmazonGamesDir(), "pendingUploadDescription.txt").delete();
      return;
    }
    writeToAmazonGamesDir("pendingUploadDescription.txt", paramPendingUpload.getDescription().getBytes());
  }

  public final void storeSummary(GameSummary paramGameSummary)
  {
    writeGameSummary(paramGameSummary, "latestSummary.txt");
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.ags.client.whispersync.savedgame.FileBackedSummaryRepository
 * JD-Core Version:    0.6.2
 */